﻿using System;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Contabil.Communication.Bovespa.Contabil.EventoContabil;

namespace Sinacor.UI.Bovespa.Contabil.Communication
{
    /// <summary>
    /// Communication Class - EventoContabil
    /// </summary>
    public class EventoContabil : BaseCommunication
    {
        #region Construtor

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="session">SessionInformation</param>
        public EventoContabil(SessionInformation session) : base(session) { }        

        #endregion

        #region Métodos Públicos

        /// <summary>
        /// Alterar evento contábil
        /// </summary>
        /// <param name="model">Model.EventoContabil</param>
        public void AlterarEventoContabil(Model.EventoContabil model)
        {
            try
            {
                using (ServiceInstance<IEventoContabil> serviceManager = new ServiceInstance<IEventoContabil>())
                {
                    IEventoContabil servico = serviceManager.TryGetInstance(this.Session);
                    servico.AlterarEventoContabil(new Translator.ModelToDataContract.EventoContabil().ToEventoContabilRequest(model));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Consultar eventos contábeis
        /// </summary>
        /// <param name="parameter">Model.Parameter.FiltroEventoContabil</param>
        /// <returns>Coleção de models do tipo Model.EventoContabil</returns>
        public ModelCollection<Model.EventoContabil> ConsultarEventosContabeis(Model.Parameter.FiltroEventoContabil parameter)
        {
            using (ServiceInstance<IEventoContabil> serviceManager = new ServiceInstance<IEventoContabil>())
            {
                IEventoContabil servico = serviceManager.TryGetInstance(this.Session);
                return new Translator.DataContractToModel.EventoContabil().ToEventoContabil(servico.ConsultarEventosContabeis(new Translator.ModelToDataContract.EventoContabil().ToFiltroEventoContabilRequest(parameter)));
            }
        }

        /// <summary>
        /// Excluir evento contábil
        /// </summary>
        /// <param name="parameter">Model.Parameter.FiltroEventoContabilChave</param>
        public void ExcluirEventoContabil(Model.Parameter.FiltroEventoContabilChave parameter)
        {
            try
            {
                using (ServiceInstance<IEventoContabil> serviceManager = new ServiceInstance<IEventoContabil>())
                {
                    IEventoContabil servico = serviceManager.TryGetInstance(this.Session);
                    servico.ExcluirEventoContabil(new Translator.ModelToDataContract.EventoContabil().ToFiltroEventoContabilChaveRequest(parameter));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Executar transações Incluir/Alterar/Excluir de eventos contábeis
        /// </summary>
        /// <param name="inclusaoList">Coleção de models do tipo Model.EventoContabil</param>
        /// <param name="exclusaoList">Coleção de models do tipo Model.Parameter.FiltroEventoContabilChave</param>
        /// <param name="alteracaoList">Coleção de models do tipo Model.EventoContabil</param>
        public void ExecutarTransacoesEventoContabil(ModelCollection<Model.EventoContabil> inclusaoList,
                                                        ModelCollection<Model.Parameter.FiltroEventoContabilChave> exclusaoList,
                                                        ModelCollection<Model.EventoContabil> alteracaoList)
        {
            try
            {
                using (ServiceInstance<IEventoContabil> serviceManager = new ServiceInstance<IEventoContabil>())
                {
                    Translator.ModelToDataContract.EventoContabil translator = new Translator.ModelToDataContract.EventoContabil();
                    IEventoContabil servico = serviceManager.TryGetInstance(this.Session);
                    servico.ExecutarTransacoesEventoContabil(translator.ToFiltroEventoContabilChaveRequest(exclusaoList),
                                                                translator.ToEventoContabilRequest(inclusaoList),
                                                                translator.ToEventoContabilRequest(alteracaoList));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Incluir evento contabil
        /// </summary>
        /// <param name="model"></param>
        public void IncluirEventoContabil(Model.EventoContabil model)
        {
            try
            {
                using (ServiceInstance<IEventoContabil> serviceManager = new ServiceInstance<IEventoContabil>())
                {
                    IEventoContabil servico = serviceManager.TryGetInstance(this.Session);
                    servico.IncluirEventoContabil(new Translator.ModelToDataContract.EventoContabil().ToEventoContabilRequest(model));
                }
            }
            catch (Exception ex)
            {                
                base.HandleFault(ex);
            } 
        }

        #endregion
    }
}